﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
using System.Windows.Media;

using NUnit.Framework;

using Uhi.Libraries.Test;
using Uhi.Libraries.Windows.Interactivity;

using Uhi.Applications.FileAid.Interactivity;
using Uhi.Applications.FileAid.View;

namespace FileAidTest.Interactivity
{
    [TestFixture]
    public class SelectTextActionTest
    {
        #region Invoke

        #region 正常ケース
        [Test, STAThread]
        public void Invoke_正常ケース()
        {
            var textFactory = new FrameworkElementFactory(typeof(TextEditor));
            textFactory.SetBinding(TextEditor.TextProperty, new Binding("."));
            textFactory.Name = "StrText";
            
            var dataTemplate = new DataTemplate();
            dataTemplate.VisualTree = textFactory;

            var listbox = new ListBox();
            listbox.ItemTemplate = dataTemplate;
            listbox.ItemsSource = new[] { "abcde", "abcde", "abcde" };

            var action = new SelectTextAction();

            var trigger = new InteractionRequestTrigger();
            trigger.Actions.Add(action);

            Interaction.GetTriggers(listbox).Add(trigger);

            var window = new Window();
            window.Content = listbox;

            window.Show();
            
            var selection = new TextSelection();
            selection.SelectionRow = 1;
            selection.SelectionColumn = 2;
            selection.SelectionStart = 2;
            selection.SelectionLength = 2;

            action.AsDynamic().Invoke(new InteractionRequestEventArgs("", selection, null));

            var container = listbox.ItemContainerGenerator.ContainerFromIndex(1) as ListBoxItem;
            var border = VisualTreeHelper.GetChild(container, 0);
            var presenter = VisualTreeHelper.GetChild(border, 0);
            var textbox = VisualTreeHelper.GetChild(presenter, 0) as TextEditor;

            textbox.CaretIndex.Is(2);

            window.Close();
        }
        #endregion

        #endregion

        #region FindVisualChild

        #region 正常ケース
        [Test, STAThread]
        public void FindVisualChild_正常ケース()
        {
            var textbox = new TextBox();
            var button = new Button();

            var panel = new StackPanel();
            panel.Children.Add(textbox);
            panel.Children.Add(button);

            var window = new Window();
            window.Content = panel;

            window.Show();

            (new SelectTextAction().AsDynamic().FindVisualChild<TextBox>(window) as object).Is(textbox);
            (new SelectTextAction().AsDynamic().FindVisualChild<Button>(window) as object).Is(button);

            window.Close();
        }
        #endregion

        #endregion
    }
}
